package ComparableTest;//ProductComparable.java

import java.util.Comparator;
import java.util.Date;

public class ProductComparable implements Comparator<Product> {

    // 对象的排序方式[升、降]   
    public static boolean sortASC = true;

    // 对象的排序属性   
    public static boolean sortByName = false;
    public static boolean sortByDate = false;
    public static boolean sortByPrice = false;


    @Override
    public int compare(Product pro1, Product pro2) {

        int result = 0;

        if (sortASC) {
            if (sortByName) {
                String pro1name = pro1.getName();
                String pro2name = pro2.getName();
                result = pro1name.compareTo(pro2name);
            } else if (sortByDate) {
                Date pro1Date = pro1.getDate();
                Date pro2Date = pro2.getDate();

                result = pro1Date.compareTo(pro2Date);

            } else if (sortByPrice) {
                Integer pro1Price = pro1.getPrice();
                Integer pro2Price = pro2.getPrice();

                result = pro1Price.compareTo(pro2Price);
            }
        } else {
            if (sortByName) {
                String pro1name = pro1.getName();
                String pro2name = pro2.getName();

                result = -pro1name.compareTo(pro2name);
            } else if (sortByDate) {
                Date pro1Date = pro1.getDate();
                Date pro2Date = pro2.getDate();
            }
        }
        return result;
    }
}